package com.leave.mos.controller.simple;

import com.leave.mos.constant.TablenameConstant;
import com.leave.mos.entity.Order;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ComparisonQueryOperatorsController
 * @Description TODO
 * @Author lh
 * @Date 2021/7/12 16:50
 * @Version 1.0
 *
 * Comparison Query Operators 比较运算符的使用
 *
 **/
@RestController
@RequestMapping("comparison")
public class ComparisonQueryOperatorsController {

    @Resource
    private MongoTemplate mongoTemplate;

    @RequestMapping("eq")
    public List<Map> eq(){

        Criteria c = new Criteria(TablenameConstant.goods_order.orderName);
        c.is("新订单10");
        Query query = new Query(c);

        List<Map> maps = mongoTemplate.find(query,Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }

    @RequestMapping("eq1")
    public List<Map> eq1(){

        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(new Criteria(TablenameConstant.goods_order.orderName).is("新订单10"))
        );
        AggregationResults<Map> aggregate = mongoTemplate.aggregate(aggregation, TablenameConstant.goods_order.TableName, Map.class);
        List<Map> mappedResults = aggregate.getMappedResults();
        return mappedResults;
    }

    // 等于
    @RequestMapping("eq2")
    public List<Order> eq2(){

        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(new Criteria(TablenameConstant.goods_order.orderName).is("新订单10"))
        );
        AggregationResults<Order> aggregate = mongoTemplate.aggregate(aggregation, TablenameConstant.goods_order.TableName, Order.class);
        List<Order> mappedResults = aggregate.getMappedResults();
        return mappedResults;
    }

    //大于
    @RequestMapping("gt")
    public List<Map> gt(){
        Criteria c = new Criteria(TablenameConstant.goods_order.status);
        //字段类型，一定要对应
        c.gt(2);
        Query query = new Query(c);

        List<Map> maps = mongoTemplate.find(query,Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }

    //大于等于
    @RequestMapping("gte")
    public List<Map> gte(){
        Criteria c = new Criteria(TablenameConstant.goods_order.status);
        //字段类型，一定要对应
        c.gte(2);
        Query query = new Query(c);

        List<Map> maps = mongoTemplate.find(query,Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }

    //小于
    @RequestMapping("lt")
    public List<Map> lt(){
        Criteria c = new Criteria(TablenameConstant.goods_order.status);
        //字段类型，一定要对应
        c.lt(2);
        Query query = new Query(c);

        List<Map> maps = mongoTemplate.find(query,Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }

    //小于等于
    @RequestMapping("lte")
    public List<Map> lte(){
        Criteria c = new Criteria(TablenameConstant.goods_order.status);
        //字段类型，一定要对应
        c.lte(2);
        Query query = new Query(c);

        List<Map> maps = mongoTemplate.find(query,Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }


    //不等于
    @RequestMapping("ne")
    public List<Map> ne(){
        Criteria c = new Criteria(TablenameConstant.goods_order.status);
        //字段类型，一定要对应
        c.ne(1);
        Query query = new Query(c);

        List<Map> maps = mongoTemplate.find(query,Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }

    //不等于 agg方法
    @RequestMapping("ne1")
    public List<Map> ne1(){
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(new Criteria(TablenameConstant.goods_order.status).ne(1))
        );
        AggregationResults<Map> aggregate = mongoTemplate.aggregate(aggregation, TablenameConstant.goods_order.TableName, Map.class);
        List<Map> mappedResults = aggregate.getMappedResults();
        return mappedResults;
    }

    //
    @RequestMapping("in")
    public List<Map> in(){
        Criteria c = new Criteria(TablenameConstant.goods_order.status);
        //字段类型，一定要对应
        c.in(2,3);
        Query query = new Query(c);

        List<Map> maps = mongoTemplate.find(query,Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }


    //agg
    @RequestMapping("in1")
    public List<Map> in1(){

        List list = new ArrayList();
        list.add(1);
        list.add(2);

        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(new Criteria(TablenameConstant.goods_order.status).in(list))
        );
        AggregationResults<Map> aggregate = mongoTemplate.aggregate(aggregation, TablenameConstant.goods_order.TableName, Map.class);
        List<Map> mappedResults = aggregate.getMappedResults();
        return mappedResults;
    }

    //该field值不在指定array 或
    //在field不存在。
    //
    @RequestMapping("nin")
    public List<Map> nin(){
        Criteria c = new Criteria(TablenameConstant.goods_order.status);
        //字段类型，一定要对应
        c.nin(1,3);
        Query query = new Query(c);

        List<Map> maps = mongoTemplate.find(query,Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }


    //agg
    @RequestMapping("nin1")
    public List<Map> nin1(){

        List list = new ArrayList();
        list.add(1);
        list.add(2);

        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(new Criteria(TablenameConstant.goods_order.status).nin(list))
        );
        AggregationResults<Map> aggregate = mongoTemplate.aggregate(aggregation, TablenameConstant.goods_order.TableName, Map.class);
        List<Map> mappedResults = aggregate.getMappedResults();
        return mappedResults;
    }
}
